package capimClientP;

import java.awt.BorderLayout;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.management.monitor.Monitor;
import javax.swing.*;

import sun.jkernel.Mutex;

/**
 * Shows the main client window
 * @author danielm pessoafpc
 *
 */
public class ClientInterface implements Runnable{
	private InetAddress ip;
	private int portNumber;
	private String userName;
	private String password;
	private JFrame mainWindow = new JFrame("Cap-In");
	private Thread runner;

	/**
	 * Default Constructor. Sets the ipaddress and start thread
	 * @param args ip address from the server
	 * @param clientPort local port to open comunication 
	 */
	public ClientInterface(String[] args,int clientPort) {
		try {
			this.ip = InetAddress.getByName(args[0]);
		} catch (UnknownHostException e) {
			System.out.println("Host not found, try again with correct name");
		}
		setPortNumber(Integer.parseInt(args[1]));
		if(runner == null){
			runner = new Thread(this);
			runner.start();
		}
	}

	/**
	 * Thread start
	 */
	@Override
	public void run(){
		Cap_in.sendToNetwork.startConnection();
		
		// Take the username and password from User
		setUserName(JOptionPane.showInputDialog(null,"What's your login: "));
		setPassword(JOptionPane.showInputDialog(null,"What's your password: "));
		
		// Send the login information
		Cap_in.postOffice.sendLogin(this.getUserName(), this.getPassword());
		
		// Visual data
		mainWindow.setSize(300,500);
		mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainWindow.setLayout(new BorderLayout());
		mainWindow.setVisible(true);
	}
	
	/**
	 * Analize received data
	 * @param Data Message received from network
	 */
	public void packReceived(String Data){
    	// Just for debug
    	// System.out.println("Dados do pacote: "+data);
    	switch(Data.charAt(0)){
    	    // Verify if it's really a login message
    		case '1':
    			// Verify if it's a response login
    			if(Data.charAt(1) == '3'){ 
    				// Check if login was ok
    				if(Data.charAt(2) == '1'){
    					System.out.println("Login realizado com Sucesso!");
    					Cap_in.postOffice.retvList(this.getUserName(), this.getPassword());
    				} else if(Data.charAt(2) == '0'){
    					System.out.println("Falha no login");
    					// Ask the login and password again
    					// TODO limit to 3 retries
    					setUserName(JOptionPane.showInputDialog(null,"What's your login: "));
    					setPassword(JOptionPane.showInputDialog(null,"What's your password: "));
    					Cap_in.postOffice.sendLogin(this.getUserName(), this.getPassword());
    				}
    			}
    			break;
    			
    		default:
    			break;
    	}
	}
	
	public void setIp(InetAddress ip) {
		this.ip = ip;
	}

	public InetAddress getIp() {
		return ip;
	}

	public void setPortNumber(int portNumber) {
		this.portNumber = portNumber;
	}

	public int getPortNumber() {
		return portNumber;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserName() {
		return userName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}
}
